﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;

namespace DBHelp
{
    public static class DBHelp
    {
        public static DBConnectionSingletion DBPool = DBConnectionSingletion.Instance;

        public static void Init()
        {
            DBPool.ConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
        }

        #region 根据SQL语句获取数据
        public static DataTable QueryDataFromDB(string SqlStr)
        {
            MySqlConnection conn = DBPool.BorrowDBConnection();
            DataTable dt = new DataTable();
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            try
            {
                adapter.SelectCommand = new MySqlCommand(SqlStr, conn);
                adapter.Fill(dt);
                return dt;
            }
            finally
            {
                if (adapter != null)
                    adapter.Dispose();
                DBPool.ReturnDBConnection(conn);
            }
        }
        #endregion


        #region 执行SQL语句
        public static string ExecSql(string sql)
        {
            MySqlConnection conn = DBPool.BorrowDBConnection();
            try
            {
                using (MySqlCommand cmd = new MySqlCommand(sql, conn))
                {
                    cmd.CommandType = CommandType.Text;
                    return cmd.ExecuteNonQuery().ToString();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("执行sql语句出错:"+ex.Message+sql);
                return "执行sql语句出错:" + ex.Message + sql;
            }
            finally
            {
                DBPool.ReturnDBConnection(conn);
            }
        }
        #endregion
        #region 执行SQL语句
        public static int ExSelectCout(string sql)
        {
            MySqlConnection conn = DBPool.BorrowDBConnection();
            try
            {
                using (MySqlCommand cmd = new MySqlCommand(sql, conn))
                {
                    cmd.CommandType = CommandType.Text;
                    return Convert.ToInt32(cmd.ExecuteScalar());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("执行sql语句出错:" + ex.Message);
                return 0;
            }
            finally
            {
                DBPool.ReturnDBConnection(conn);
            }
        }
        #endregion

        public static object executeSql(string sql)
        {
            MySqlConnection conn = DBPool.BorrowDBConnection();
            try
            {
                using (MySqlCommand cmd = new MySqlCommand(sql, conn))
                {
                    cmd.CommandType = CommandType.Text;
                    return cmd.ExecuteScalar();
                }
            }
            finally
            {
                DBPool.ReturnDBConnection(conn);
            }
        }

 
    

    }
}
